//
//  AddImgView.m
//  IceBoxStrorage
//
//  Created by xieyanhong on 2023/8/21.
//

#import "AddImgView.h"
#import "AddTool.h"
#import <TZImagePickerController/TZImagePickerController.h>
@implementation AddImgView

-(instancetype)init{
    if (self = [super initWithFrame:CGRectZero]) {
        [self initSubView];
    }
    
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initSubView];
    }
    return self;
}

- (void)imageAct:(UIButton *)button{
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
    imagePickerVc.photoWidth = 200;
    imagePickerVc.photoPreviewMaxWidth = 200;
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowPickingImage = YES;
    __weak typeof(self)weakSelf = self;
    imagePickerVc.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if(photos.count){
            UIImage *image = photos.firstObject;
            NSString *cover = [AddTool saveImg:image];
            weakSelf.coverFile = cover;
            [button setImage:image forState:0];
        }
    };
    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [[AddTool currentVC] presentViewController:imagePickerVc animated:YES completion:nil];
    
}


- (void)initSubView{
    
    self.coverFile = @"";
    UILabel *titleLabel = [[UILabel alloc]init];
    [self addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(@16);
    }];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textColor = UIColor.blackColor;
    self.nameLabel = titleLabel;
    
    UIButton *deviceButton = [[UIButton alloc]init];
    [self addSubview:deviceButton];
    [deviceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-30);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    [deviceButton setTitle:@"+" forState:0];
    [deviceButton setTitleColor:[UIColor whiteColor] forState:0];
    deviceButton.titleLabel.font = [UIFont systemFontOfSize:40];
    deviceButton.layer.cornerRadius = 4;
    deviceButton.layer.masksToBounds = YES;
    deviceButton.backgroundColor = rgba(145, 145, 146, 1);
    [deviceButton addTarget:self action:@selector(imageAct:) forControlEvents:UIControlEventTouchUpInside];
    self.imageBtn = deviceButton;
    
    
}

@end
